import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class JedisDemoList {
    public static void test1(Jedis jedis) {
        //lpuhs rpush
        //lpop rpop
        //blpop brpop
        //lrange
        jedis.flushAll();
        jedis.lpush("list1", "1", "2", "3", "4", "5", "6", "7", "8");
        System.out.println(jedis.lrange("list1", 0, -1));
        jedis.del("list1");
        jedis.rpush("list1", "1", "2", "3", "4", "5", "6", "7", "8");
        System.out.println(jedis.lrange("list1", 0, -1));
    }
    public static <Stirng> void test2(Jedis jedis) {
        jedis.flushAll();
        jedis.lpush("list1", "1", "2", "3", "4", "5", "6", "7", "8");
        String list1 = jedis.lpop("list1");
        System.out.println(list1);
        String list2= jedis.rpop("list1");
        System.out.println(list2);
    }
    public static void  test3(Jedis jedis) {
        jedis.flushAll();
        //这里返回的是一个二元组，前半部分是一个从那个key里面删除的，后面是删除的数据
        //rpop跟这个是一样的
        //如果列表里面没有元素的话，就会进行阻塞
        List<String> list = jedis.blpop(100, "key");
        System.out.println("key: "+list.get(0));
        System.out.println("value: "+list.get(1));
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://localhost:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            jedis.auth("maihao1122");
            //test1(jedis);
            //test2(jedis);
            test3(jedis);
        }
    }
}
